package com.cenxi.ipdemo1.utils;


import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;

public class LocalDateUtil {

    /**
     * 是否日期重叠
     * @param start1
     * @param end1
     * @param start2
     * @param end2
     * @return
     */
    public static Boolean isOverlap(int start1,int end1,int start2,int end2){
        if (start1 <= end1 && start2<=end2) {
            //都不跨年
            return !(end1 < start2 || end2 < start1);
        }else if(start1 > end1 && start2 <=end2){
            //范围1跨年
            return (start2<=start1&&end2>=start1)||(start2<=end1&&end2>=end1)||start2>=start1||end2<=end1;
        } else if(start1 <= end1 && start2 > end2){
            //范围2跨年
            return (start1<=start2&&end1>=start2)||(start1<=end2&&end1>=end2)||start1>=start2||end1<=end2;
        }else{
            //都跨年
            return true;
        }
    }

    /**
     * 获取当前月日在一年的第几天
     * @param month
     * @param day
     * @return
     */
    public static  int getDayOfYear(int month, int day) {
        int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int days = 0;
        for (int i = 0; i < month - 1; i++) {
            days += daysInMonth[i];
        }
        return days + day;
    }

    /**
     * 转换时间戳
     * @return
     */
    public static long getLongTime(LocalDateTime time) {
        return time.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static List<LocalDate> getMergeListTime(LocalDate startDay, LocalDate endDay) {
        List<LocalDate> arr = new ArrayList<>();
        while (startDay.isBefore(endDay) || startDay.equals(endDay)) {
            arr.add(startDay);
            startDay = startDay.plusDays(1);
        }
        return arr;
    }

    /**
     * 判断两个时间范围是否有交集
     *
     * @param dynaStartTime  比较时间段开始时间
     * @param dynaEndTime    比较时间段结束时间
     * @param fixedStartTime 参考时间段开始时间
     * @param fixedEndTime   参考时间段结束时间
     * @return
     */
    public static Boolean checkTimesHasOverlap(long dynaStartTime, long dynaEndTime, long fixedStartTime, long fixedEndTime) {
        return !(dynaEndTime < fixedStartTime || dynaStartTime > fixedEndTime);
    }

    /**
     * 获取时间范围所有数据集合
     *
     * @param start
     * @param end
     * @return
     */
    public static List<LocalDate> getTimeHorizonList(LocalDate start, LocalDate end) {
        List<LocalDate> resList = new ArrayList<>();
        while (start.isBefore(end) || start.equals(end)) {
            resList.add(start);
            start = start.plusDays(1);
        }
        return resList;
    }

    /**
     * 获取上周第一天和最后一天
     * param monthOrWeekType 1:月 2:周
     */
    public static List<LocalDate> getFirstLastWeekOrFirstLastMonth(Integer monthOrWeekType) {
        List<LocalDate> resList = new ArrayList<>();
        LocalDate today = LocalDate.now();
        if (monthOrWeekType == 2) {
            today = today.minusWeeks(1);
            LocalDate start = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            LocalDate end = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            resList.add(start);
            resList.add(end);
        }
        if (monthOrWeekType == 1) {
            today = today.minusMonths(1);
            LocalDate start = today.with(TemporalAdjusters.firstDayOfMonth());
            LocalDate end = today.with(TemporalAdjusters.lastDayOfMonth());
            resList.add(start);
            resList.add(end);
        }
        return resList;
    }
}
